package leetcode.q203;

import leetcode.ListNode;

/**
 * <a href="https://leetcode.cn/problems/remove-linked-list-elements/description/">...</a>
 * 203. 移除链表元素
 */
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while(head != null && head.val == val) {
            head = head.next;
        }

        if (head == null) return head;

        ListNode prev = head;
        while (prev.next != null) {
            if (prev.next.val == val) {
                ListNode node = prev.next;
                prev.next = node.next;
                node.next = null;
                continue;
            }
            prev = prev.next;
        }

        return head;
    }
}